// pages/user/Resume/Resume.js

import api from '../../../api/index'
import create from '../../../utils/create'
import store from '../../../store/index'

create(store, {

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [
      "cloud://cloud1-6gvg8052f9670276.636c-cloud1-6gvg8052f9670276-1305457435/05b34594-d90d-4773-ab94-91bebc2c5f3b.docx"
    ],
    works: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      works: store.data.user.works
    })
  },
  tapAttach: function () {
    // 我的简历数小于5时，才可以上传
    if (store.fileList.worksNum < 5) {
      var self = this;
      api.file.uploadWorks().then(res => {
        console.log(res)
        const that = self
        store.refreshUser().then(res => {
          console.log(res);
          store.fileList.worksNum = res.works.length;
          that.setData({
            works: res.works
          })
        })
      })
    } else {
      wx.showToast({
        title: '文件数已达上限',
        icon: 'error'
      })
    }

  },

  // 刪除所有文件
  deleteAll: function () {
    var file = store.data.user.works.file
    var params = { file }
    var self = this
    wx.showActionSheet({
      itemList: ['确定要删除所有作品吗'],//显示的列表项
      success: function (res) {//res.tapIndex点击的列表项
        console.log("执行删除操作")
        const that = self
        api.file.del2(params).then(res => {
          console.log(res)
          store.refreshUser().then(res => {
            that.setData({
              resume: res.resume,
              works: res.works
            })
          })
        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })

  },

  open(e) {
    var index = e.currentTarget.dataset.index
    var file = store.data.user.works[index].file
    var fileList = []
    fileList.push(file)
    const params = { fileList }

    wx.showLoading({ title: '打开中' })
    api.file.downloadResume(params).then(res => {
      console.log(res.massage)
      wx.hideLoading()
    })
  },
  more: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var file = store.data.user.works[index].file
    var params = { file }
    var self = this
    wx.showActionSheet({
      itemList: ['删除'],//显示的列表项
      success: function (res) {//res.tapIndex点击的列表项
        console.log("执行删除操作")
        const that = self
        api.file.del2(params).then(res => {
          console.log(res)
          store.refreshUser().then(res => {
            that.setData({
              works: res.works
            })
          })

        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },


})